package com.example.Utils;

import com.example.Entity.Vo.APIResult;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HttpRequestUtils {
    public static String sendHttpRequest(String method, String url, Map<String, String> headers, String body) throws IOException {
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        try {
            URL apiUrl = new URL(url);
            connection = (HttpURLConnection) apiUrl.openConnection();
            connection.setRequestMethod(method);
            // 设置请求头
            if (headers != null) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    connection.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
            // 设置请求体
            if (body != null && !body.isEmpty()) {
                connection.setDoOutput(true);
                try (OutputStream os = connection.getOutputStream()) {
                    byte[] input = body.getBytes("utf-8");
                    os.write(input, 0, input.length);
                }
            }
            int responseCode = connection.getResponseCode();
            if (responseCode >= 200 && responseCode < 300) {
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            } else {
                reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
            }
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            String resource = response.toString();
            // 关闭资源
            if (reader != null) {
                reader.close();
            }
            if (connection != null) {
                connection.disconnect();
            }
//            ObjectMapper objectMapper = new ObjectMapper();
//            APIResult apiResult = objectMapper.readValue(resource, APIResult.class);
            return resource;
        } catch (IOException e) {
            e.printStackTrace();
            throw e; // 或者返回适当的错误处理
        }
    }

    public static String sendHttpGetRequest(String url, Map<String, String> headers, String body) throws IOException {
        return sendHttpRequest("GET", url, headers, body);
    }

    public static String sendHttpGetRequest(String url, String body) throws IOException {
        return sendHttpRequest("GET", url, null, body);
    }

    public static String sendHttpGetRequest(String url, Map<String, String> headers) throws IOException {
        return sendHttpRequest("GET", url, headers, null);
    }

    public static String sendHttpGetRequest(String url) throws IOException {
        return sendHttpRequest("GET", url, null, null);
    }

    public static String sendHttpPostRequest(String url, Map<String, String> headers, String body) throws IOException {
        return sendHttpRequest("POST", url, headers, body);
    }

    public static String sendHttpPostRequest(String url, String body) throws IOException {
        return sendHttpRequest("POST", url, null, body);
    }

    public static String sendHttpPostRequest(String url, Map<String, String> headers) throws IOException {
        return sendHttpRequest("POST", url, headers, null);
    }

    public static String sendHttpPostRequest(String url) throws IOException {
        return sendHttpRequest("POST", url, null, null);
    }
}
